package rise.ui;

import rise.comunity.Community;
import rise.comunity.UserData;
import rise.message.Message;
import rise.net.MessageTask;
import rise.net.OlivawPortsClient;
import rise.net.ReceiverService;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class Conversation extends RiseActivity {

	private MessageAdapter messageAdapter;
	private String sender;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_conversation);
		Conversation.conversation = this;

		Intent inputIntent = this.getIntent();
		sender = inputIntent.getExtras().getString("username");
		this.setTitle(sender);

		ListView messagesView = (ListView) findViewById(R.id.messages);
		messageAdapter = new MessageAdapter(this, R.id.messages, sender);
		messagesView.setAdapter(messageAdapter);
	}

	@Override
	public void onBackPressed() {
		ReceiverService receiverService = ReceiverService
				.obtainReceiverService();
		if (receiverService == null || !receiverService.isRunning()) {
			super.exitFromRise();
			return;
		}/*
		 * else{ Intent serviceIntent = new Intent(this, ReceiverService.class);
		 * serviceIntent.setAction("rise.net.ReceiverService");
		 * this.startService(serviceIntent); }
		 */
		super.onBackPressed();
	}

	public void sendMessage(View v) {
		EditText editText = (EditText) this.findViewById(R.id.nuovoMessaggio);
		String text = editText.getText().toString();
		if (!checkText(text)) {
			this.handleInvalidMessage();
			return;
		}

		UserData userData = UserData.obtainUserData();
		
		text = text.replaceAll("\t", " ");
		text = text.replaceAll("\n", " ");
		if(sender.equals(Community.broadcastWildcard))
			text = "[Broadcast] "+text;
		Message message = new Message(userData.getUsername(), sender, text,
				OlivawPortsClient.getInputPort(), 0, 0, userData.getStatus());
		new MessageTask(message, this).execute(new Void[0]);

		editText.setText("");
		Community community = Community.obtainCommunity();
		community.flushConversationFrom(sender);
	}

	private void handleInvalidMessage() {
		Toast toast = Toast
				.makeText(
						this,
						"Il messaggio è vuoto oppure contiene uno dei seguenti caratteri: '<','>','//'.",
						Toast.LENGTH_SHORT);
		toast.show();
	}

	private boolean checkText(String text) {
		if (text == null || text.length() == 0)
			return false;
		if (text.contains("<") || text.contains(">") || text.contains("/"))
			return false;
		return true;
	}

	@Override
	protected void onResume() {
		super.onResume();
		Conversation.refreshConversationActivity();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		Conversation.refreshConversationActivity();
	}

	private static Conversation conversation;
	public static ConversationRefreshHandler handler;

	public synchronized static void refreshConversationActivity() {
		if (conversation == null)
			return;
		Community c = Community.obtainCommunity();
		conversation.messageAdapter.clear();
		conversation.messageAdapter.addAll(c
				.getMessagesFrom(conversation.sender));
	}
}
